//
//  OnlineMenuView.m
//  Molian
//
//  Created by 小蚕 on 15/9/4.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "OnlineMenuView.h"
#import "ZHPickView.h"
#define kMyBtTag 156
#define kBgViewHeight 170

@interface OnlineMenuView ()<ZHPickViewDelegate>
{
    UIView *bgView;
    UIView *lineView;
    UIView *lineView2;
    UIButton *leftBt;
    UIButton *rightBt;
}
//@property (nonatomic, strong)NSArray *titleArr;
//@property (nonatomic, strong)NSArray *imageTitle;
//@property (nonatomic, strong) UILabel *hintLabel;
@property (nonatomic, strong) UIButton *cityBt;
@property(nonatomic,strong)ZHPickView *pickview;

@end

@implementation OnlineMenuView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 0;
        self.backgroundColor = RGBA(1, 1, 1, 0.3);
        
        
        
        //        bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, DeviceWidth, kBgViewHeight)];
        bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, DeviceWidth, kBgViewHeight)];
        bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:bgView];
        
        UIView *dismissView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight+bgView.frame.size.height, DeviceWidth, DeviceHeight-kNavigationBarHeight-kBgViewHeight)];
        dismissView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBuyView)];
        [dismissView addGestureRecognizer:tap];
        [self addSubview:dismissView];
        NSDictionary *dic = [Utils screenRecord];
        if (dic) {
            self.gender = [[dic objectForKey:@"sex"] integerValue];
            self.city = [dic objectForKey:@"city"];
            // self.userType = [[dic objectForKey:@"userType"] integerValue];//筛选普通用户
        }else{
            self.gender = 0;
            // self.userType = 0;//筛选普通用户
        }
        UIColor *labelColor = RGBA(135, 135, 135, 1);
        UIColor *btBackgroundColor = RGBA(242, 242, 242, 1);
        UIColor *btTextColor = RGBA(76, 78, 70, 1);
        
        //  NSArray *arr = [NSArray arrayWithObjects:@"全部",@"男生",@"女生",@"登录时间",@"距离", nil];
        NSArray *arr = [NSArray arrayWithObjects:@"全部",@"男生",@"女生", nil];
        //  for (int i = 0; i<2; i++) {
        int  i= 0;
        UILabel *genderlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 8+i*76, 150, 15)];
        genderlabel.textColor = labelColor;
        genderlabel.font = kFont(14);
        // if (i == 0) {
        genderlabel.text = @"性别";
        //            }else{
        //                genderlabel.text = @"排序方式";
        //
        //                self.hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 8+i*76+76, DeviceWidth - 20, 15)];
        //                self.hintLabel.textColor = labelColor;
        //                self.hintLabel.font = kFont(14);
        //                [bgView addSubview:self.hintLabel];
        
        
        //    }
        
        
        
        
        
        
        [bgView addSubview:genderlabel];
        
        UIView *btBGView = [[UIView alloc] initWithFrame:CGRectMake(10, 32.5+i*(105-32.5), DeviceWidth-20, 44)];
        CGFloat h = btBGView.frame.size.height;
        CGFloat w = btBGView.frame.size.width;
        btBGView.backgroundColor = btBackgroundColor;
        btBGView.layer.masksToBounds = YES;
        btBGView.layer.cornerRadius = 5;
        [bgView addSubview:btBGView];
        
        NSInteger count = 2;
        if (i == 0) {
            count = 3;
        }else{
            count = 2;
        }
        
        for (int j = 0; j<count; j++) {
            UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
            bt.frame = CGRectMake(w/3/2-80/2+j*w/3, h/2-28/2, 80, 28);
            bt.layer.masksToBounds = YES;
            bt.layer.cornerRadius = 4;
            [bt setTitle:arr[i*3+j] forState:UIControlStateNormal];
            bt.tag = kMyBtTag +i*3+j;
            bt.titleLabel.font = kFont(16);
            if (i == 0&&j == self.gender ) {
                [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                bt.backgroundColor = APPCOLOR(1);
            }else if(i == 1){
                //                    if (self.userType == 0&&j == 0) {
                //                        [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                //                        bt.backgroundColor = APPCOLOR(1);
                //                    }else if(self.userType == 1&&j == 1){
                //                        [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                //                        bt.backgroundColor = APPCOLOR(1);
                //                    }else if(self.userType == 0&&j == 2){
                //                        [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                //                        bt.backgroundColor = APPCOLOR(1);
                //                    }else{
                //                         [bt setTitleColor:btTextColor forState:UIControlStateNormal];
                //                        bt.backgroundColor = [UIColor clearColor];
                //                    }
            }else{
                [bt setTitleColor:btTextColor forState:UIControlStateNormal];
                bt.backgroundColor = [UIColor clearColor];
            }
            [bt addTarget:self action:@selector(clickBt:) forControlEvents:UIControlEventTouchUpInside];
            [btBGView addSubview:bt];
        }
        //  }
        //新增,城市选择
        
        UILabel *workLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, kBgViewHeight - 90, 120, 40)];
        workLabel.textColor = RGBA(80, 80, 80, 1);
        workLabel.font = kFont(15);
        workLabel.textAlignment = NSTextAlignmentLeft;
        workLabel.text = @"工作生活中";
        [bgView addSubview:workLabel];
        
        
        
        
        
        
        self.cityBt = [UIButton buttonWithType:UIButtonTypeCustom];
        self.cityBt.frame = CGRectMake(140, kBgViewHeight - 90, DeviceWidth - 160, 40);
        [self.cityBt setTitleColor:RGBA(180, 180,180, 1) forState:UIControlStateNormal];
        [self.cityBt setTitleColor:RGBA(222, 222, 222, 1) forState:UIControlStateHighlighted];
      
        self.cityBt.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        
        self.cityBt.titleLabel.font = kFont(13);
        
        [self.cityBt addTarget:self action:@selector(clickCityBt) forControlEvents:UIControlEventTouchUpInside];
      
        if (self.city == nil ||self.city.length == 0) {
            self.city = @"不限";
        }
        [self.cityBt setTitle:_city forState:UIControlStateNormal];
        [bgView addSubview:self.cityBt];
        
        //        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(10, 164, DeviceWidth - 20, 32)];
        //        myView.backgroundColor = [UIColor clearColor];
        //        [bgView addSubview:myView];
        //        CGFloat h = myView.frame.size.width;
        //        for (int i = 0; i<2; i++) {
        //            for (int j = 0; j<3; j++) {
        //                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((h/3-100)/2+j*h/3, i*(24), 16, 16)];
        //                imageView.hidden = YES;
        //                imageView.tag = kMyBtTag +10+i*3+j;
        //                [myView addSubview:imageView];
        //                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(imageView.frame.origin.x + 0, imageView.frame.origin.y, 74, 16)];
        //                label.textColor = labelColor;
        //                label.hidden = YES;
        //                label.tag = kMyBtTag +20+i*3+j;
        //                label.font = kFont(14);
        //                [myView addSubview:label];
        //
        //            }
        //        }
        [self changeTitleArrAndImageArr];
        
        
        lineView = [[UIView alloc] initWithFrame:CGRectMake(0, bgView.frame.size.height - 50.5, DeviceWidth, 0.5)];
        lineView.backgroundColor = RGBA(200, 200, 200, 1);
        [bgView addSubview:lineView];
        
        lineView2 = [[UIView alloc] initWithFrame:CGRectMake(DeviceWidth/2-0.25, bgView.frame.size.height - 50.5+10, 0.5, 30)];
        lineView2.backgroundColor = RGBA(200, 200, 200, 1);
        [bgView addSubview:lineView2];
        
        leftBt = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBt.frame = CGRectMake(0, bgView.frame.size.height-50, DeviceWidth/2, 50);
        [leftBt setTitle:@"取消" forState:UIControlStateNormal];
        [leftBt addTarget:self action:@selector(clickBuyViewLeftBt) forControlEvents:UIControlEventTouchUpInside];
        leftBt.titleLabel.font = kFont(15);
        [leftBt setTitleColor:RGBA(33, 33, 33, 1) forState:UIControlStateNormal];
        [leftBt setTitleColor:RGBA(188, 188, 188, 1) forState:UIControlStateHighlighted];
        [bgView addSubview:leftBt];
        
        rightBt = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBt.frame = CGRectMake(DeviceWidth/2, bgView.frame.size.height-50, DeviceWidth/2, 50);
        [rightBt setTitle:@"确定" forState:UIControlStateNormal];
        [rightBt addTarget:self action:@selector(clickBuyViewRightBt) forControlEvents:UIControlEventTouchUpInside];
        rightBt.titleLabel.font = kFont(15);
        [rightBt setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
        [rightBt setTitleColor:RGBA(188, 188, 188, 1) forState:UIControlStateHighlighted];
        [bgView addSubview:rightBt];
        
    }
    return self;
}
- (void)clickCityBt
{
    _pickview=[[ZHPickView alloc] initPickviewWithPlistName:@"cityAddWhole" isHaveNavControler:NO];
    _pickview.delegate = self;
    [_pickview show];
}
//选择控件返回内容后赋值
-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(id )resultString
{
    self.city = resultString[1];
}
    
- (void)changeTitleArrAndImageArr
{
    CGFloat h = 0;
    //    if (self.userType == 0) {
    //        h = 30;
    //        self.hintLabel.text = @"按会员登录时间排";
    //      //  self.titleArr = [NSArray arrayWithObjects:@"注册用户",nil];
    //      //  self.imageTitle = [NSArray arrayWithObjects:@"注册用户",nil];
    //    }else if(self.userType == 1){
    //        h = 30;
    //         self.hintLabel.text = @"按距离排, 但不显示具体数字";
    //        //self.titleArr = [NSArray arrayWithObjects:@"真人认证", @"出租时间",@"1v1陪伴",@"高颜值",@"200金币/天",nil];
    //     //   self.imageTitle = [NSArray arrayWithObjects:@"真人认证", @"出租时间",@"1v1陪伴",@"高颜值",@"金币-天",nil];
    //    }else{
    //        h = 30;
    //        self.hintLabel.text = @"按会员登录时间排";
    //      //  self.titleArr = [NSArray arrayWithObjects:@"真人认证", @"出租时间",@"1v1陪伴",@"高颜值",@"300金币/天",@"恋人故事",nil];
    //     //   self.imageTitle = [NSArray arrayWithObjects:@"真人认证", @"出租时间",@"1v1陪伴",@"高颜值",@"金币-天",@"恋人故事",nil];
    //    }
    bgView.frame = CGRectMake(0, kNavigationBarHeight, DeviceWidth, kBgViewHeight-h);
    lineView.frame = CGRectMake(0, bgView.frame.size.height - 50.5, DeviceWidth, 0.5);
    lineView2.frame = CGRectMake(DeviceWidth/2-0.25, bgView.frame.size.height - 50.5+10, 0.5, 30);
    
    leftBt.frame = CGRectMake(0, bgView.frame.size.height-50, DeviceWidth/2, 50);
    
    rightBt.frame = CGRectMake(DeviceWidth/2, bgView.frame.size.height-50, DeviceWidth/2, 50);
    [self changeTitleAndImage];
}
- (void)changeTitleAndImage
{
    
    //    for (int i = 0; i<6; i++) {
    //       // UIImageView *imageView = (UIImageView *)[self viewWithTag:kMyBtTag+10+i];
    //        UILabel *label = (UILabel *)[self viewWithTag:kMyBtTag+20+i];
    //        if (i<self.titleArr.count) {
    //        //    imageView.hidden = NO;
    //            label.hidden = NO;
    //
    //            if(i == 4){
    //                NSString *str = self.titleArr[i];
    //                NSMutableAttributedString *mString = [[NSMutableAttributedString alloc] initWithString:str];
    //                //[mString addAttribute:NSForegroundColorAttributeName value:APPCOLOR(1) range:NSMakeRange(str.length-4,4)];
    //                [mString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:10] range:NSMakeRange(str.length-4,4)];
    //                label.attributedText = mString ;
    //            }else{
    //                label.text = self.titleArr[i];
    //            }
    //
    //
    //
    //          //  imageView.image = [UIImage imageNamed:self.imageTitle[i]];
    //        }else{
    //         //   imageView.hidden = YES;
    //            label.hidden = YES;
    //        }
    //    }
}
- (void)clickBuyView
{
    if (self.alpha == 0) {
        [Utils Hidden:self Bool:YES];
    }else{
        [Utils Hidden:self Bool:NO];
    }
     [_pickview remove];
}
- (void)clickBuyViewLeftBt
{
     [_pickview remove];
    [Utils Hidden:self Bool:NO];
}

//列表为空,默认城市为全国
-(void)cityNoHuman
{
    NSInteger gender = 0;
    NSString *city = @"不限";
    
    NSDictionary *dic = [Utils screenRecord];
    if (dic) {
        gender = [[dic objectForKey:@"sex"] integerValue];
        // self.userType = [[dic objectForKey:@"userType"] integerValue];//筛选普通用户
    }
    self.city = city;
     [Utils screenRecordSex:gender city:city];
}

- (void)clickBuyViewRightBt
{
    
    // [Utils screenRecordSex:self.gender userType:self.userType];
    [Utils screenRecordSex:self.gender city:self.city];
    if ([self.delegate respondsToSelector:@selector(clickListButton)]) {
        [self.delegate clickListButton];
    }
     [_pickview remove];
    [Utils Hidden:self Bool:NO];
}
- (void)clickBt:(UIButton *)button
{
    UIButton *bt = (UIButton *)[self viewWithTag:kMyBtTag];
    UIButton *bt1 = (UIButton *)[self viewWithTag:kMyBtTag+1];
    UIButton *bt2 = (UIButton *)[self viewWithTag:kMyBtTag+2];
    //    UIButton *bt3 = (UIButton *)[self viewWithTag:kMyBtTag+3];
    //    UIButton *bt4 = (UIButton *)[self viewWithTag:kMyBtTag+4];
    //  UIButton *bt5 = (UIButton *)[self viewWithTag:kMyBtTag+5];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = APPCOLOR(1);
    UIColor *btTextColor = RGBA(76, 78, 70, 1);
    switch (button.tag) {
        case kMyBtTag:
        {
            self.gender = 0;
            [bt1 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt1.backgroundColor = [UIColor clearColor];
            [bt2 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt2.backgroundColor = [UIColor clearColor];
            
        }
            
            //全部
            break;
        case kMyBtTag+1:
            self.gender = 1;
            //男生
            [bt setTitleColor:btTextColor forState:UIControlStateNormal];
            bt.backgroundColor = [UIColor clearColor];
            [bt2 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt2.backgroundColor = [UIColor clearColor];
            break;
        case kMyBtTag+2:
            //女生
            self.gender = 2;
            [bt1 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt1.backgroundColor = [UIColor clearColor];
            [bt setTitleColor:btTextColor forState:UIControlStateNormal];
            bt.backgroundColor = [UIColor clearColor];
            break;
            //        case kMyBtTag+3:
            //            self.userType = 0;
            //
            //            //梦想恋人
            //            [bt4 setTitleColor:btTextColor forState:UIControlStateNormal];
            //            bt4.backgroundColor = [UIColor clearColor];
            //       //     [bt5 setTitleColor:btTextColor forState:UIControlStateNormal];
            //        //    bt5.backgroundColor = [UIColor clearColor];
            //            break;
            //        case kMyBtTag+4:
            //            self.userType = 1;
            //
            //            //专属恋人
            //            [bt3 setTitleColor:btTextColor forState:UIControlStateNormal];
            //            bt3.backgroundColor = [UIColor clearColor];
            //         //   [bt5 setTitleColor:btTextColor forState:UIControlStateNormal];
            //         //   bt5.backgroundColor = [UIColor clearColor];
            //            break;
            //        case kMyBtTag+5:
            //            self.userType = 0;
            //           
            //            //实习恋人
            //            [bt4 setTitleColor:btTextColor forState:UIControlStateNormal];
            //            bt4.backgroundColor = [UIColor clearColor];
            //            [bt3 setTitleColor:btTextColor forState:UIControlStateNormal];
            //            bt3.backgroundColor = [UIColor clearColor];
            //            break;
            
            
        default:
            break;
    }
    [self changeTitleArrAndImageArr];
}
- (void)dealloc
{
     [_pickview remove];
    self.delegate = nil;
}
- (void)setCity:(NSString *)city
{
    _city = city;
    
    [self.cityBt setTitle:city forState:UIControlStateNormal];
}
@end
